package o2o.web.actions.divide;

import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.divide.DividingInfoMemory;
import o2o.dao.PO.divide.DIVIDING_CODE;
import o2o.dao.PO.divide.DividingInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DividingMan {

    // 获取城市配置 若没有则或系统默认的
    public static DividingInfo getShopPacketDividingInfoByCity(String city) {
        if (ValidateUtil.isNull(city)) {
            return DividingInfoMemory.getInstance().getDefaultShopPacketShareDistributeInfo();
        }
        DividingInfo info = DividingInfoMemory.getInstance().getShopPacketShareDistributeInfoByCity(city, DIVIDING_CODE.SHOP_PACKET);
        if (info == null) {
            info = DividingInfoMemory.getInstance().getDefaultShopPacketShareDistributeInfo();
        }
        return info;
    }

    // 获取抽佣
    public static List<DividingInfo> listPlateOrShopChouYongDivide(Long shopId, Long agentId, Integer code) {
        if (ValidateUtil.isNotID(agentId)) {
            agentId = -1L;
        }
        List<DividingInfo> dividingInfoList = new ArrayList<>();
        // 先获取商家的
        if (ValidateUtil.isID(shopId)) {
            dividingInfoList = DividingInfoMemory.getInstance().listOfDivideByShopId(shopId, code);
        }
        // 商家没有 在获取商圈的
        if (!ValidateUtil.isAbsList(dividingInfoList)) {
            dividingInfoList = DividingInfoMemory.getInstance().listOfDivide(null, agentId, code);
        }
        // 在获取默认的
        if (!ValidateUtil.isAbsList(dividingInfoList) && agentId - (-1L) != 0) {
            dividingInfoList = DividingInfoMemory.getInstance().listOfDivide(null, -1L, code);
        }
        if (!ValidateUtil.isAbsList(dividingInfoList)) {
            return Collections.emptyList();
        }
        Collections.sort(dividingInfoList, Comparator.comparingDouble(DividingInfo::getMoneyRang).reversed());
        return dividingInfoList;
    }

}